package service;

import domain.Item;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xbd
 * @create 2021-06-03 14:21
 */
public class ItemService {
    private Item[] items;

    private final String ITEM_FILENAME = "Items.txt";
    private final String ANSWER_FILENAME = "answer.dat";
    private final int LINES_PER_ITEM = 6;
    public int TOTAL_ITEMS;

    public ItemService(){
        List<String> list = readTextFile(ITEM_FILENAME);
        TOTAL_ITEMS = list.size()/LINES_PER_ITEM;

        items = new Item[TOTAL_ITEMS];

        for (int i = 0; i < TOTAL_ITEMS; i++) {
            String question = list.get(i * LINES_PER_ITEM);
            String[] options = {
              list.get(i * LINES_PER_ITEM + 1),
              list.get(i * LINES_PER_ITEM + 2),
              list.get(i * LINES_PER_ITEM + 3),
              list.get(i * LINES_PER_ITEM + 4)
            };
            char answer = list.get(i * LINES_PER_ITEM + 5).charAt(0);
            items[i] = new Item(question,options,answer);
        }
    }

    private List<String> readTextFile(String filename) {
        ArrayList<String> content = new ArrayList<>();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(filename));
            String line;
            while ((line = br.readLine()) != null) {
                if (!line.trim().equals("")) {
                    content.add(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return content;
    }

    public Item getItem(int no){
        if(no < 0 || no > TOTAL_ITEMS){
            return null;
        }
        return items[no - 1];
    }

    public void saveAnswer(char[] answer){

        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(ANSWER_FILENAME));
            oos.writeObject(answer);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            if(oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public char[] readAnswer(){
        char[] answer = null;
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(ANSWER_FILENAME));
            answer = (char[]) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return answer;
    }
}
